var url = location.search; //获取url中"?"符后的字串 ('?modFlag=business&role=1')
 var theRequest = new Object();
 var id ='';
 if (url.indexOf('?') != -1) {
	 var str = url.substr(1); //substr()方法返回从参数值开始到结束的字符串；
	 var strs = str.split('&');
		 for (var i = 0; i < strs.length; i++) {
		    theRequest[strs[i].split('=')[0]] = strs[i].split('=')[1]
		 }
		 id=theRequest.id; //此时的theRequest就是我们需要的参数；
		 console.log(id)
 }
var piao;
var nameId;
var sign_up_time;
 $(function(){
	 $.ajax({
		 url:'../matchController/getMatchContent',
		 data:{
			 "id":id
		 },
		 success:function(data){
			 	nameId=data.name_id;
			 	$(".back").attr("onclick","pay('"+nameId+"')")
				$("#name").html(data.name);
			 	$(".ticket").html(data.ticket);
			 	$(".piao").html(data.countTicket);
			 	piao=data.countTicket;
			 	if(new Date() > new Date(data.sign_end_time) && new Date() < new Date(data.start_time)){
			 		if(data.in_home_url!=null && data.in_home_url!="" && data.countSignUp>0){
			 			$(".signUP").unbind("click");
			 			$(".signUP").css("background-color","red");
				 		$(".signUP").html("<a href='"+data.in_home_url+"' style='color:white' onClick='updateStart()'>进 入 房 间</a>");
			 		}else{
			 			$(".signUP").unbind("click");
				 		$(".signUP").css("background-color","#cdcdcd");
				 		$(".signUP").html(" 报 名 结 束 ");
			 		}
			 	}
			 	if(new Date() < new Date(data.sign_end_time) && data.countSignUp>0){
			 		$(".signUP").unbind("click");
			 		$(".signUP").css("background-color","#cdcdcd");
			 		$(".signUP").html(" 已 报 名 ");
			 	}
			 	if(new Date() > new Date(data.start_time)){
			 		$(".signUP").unbind("click");
			 		$(".signUP").css("background-color","#cdcdcd");
			 		$(".signUP").html(" 比 赛 进 行 中 ");
			 	}
			 	if(new Date() > new Date(data.start_time) && data.countSignUp>0){
			 		$(".signUP").unbind("click");
			 		$(".signUP").css("background-color","#cdcdcd");
			 		$(".signUP").html(" 比 赛 进 行 中 ");
			 	}
			 	if(data.end_time!=null){
			 		if(new Date() > new Date(data.end_time)){
				 		$(".signUP").unbind("click");
				 		$(".signUP").css("background-color","#cdcdcd");
				 		$(".signUP").html(" 比 赛 结 束 ");
				 	}
				 	if(new Date() > new Date(data.end_time) && data.countSignUp>0){
				 		$(".signUP").unbind("click");
				 		$(".signUP").css("background-color","#cdcdcd");
				 		$(".signUP").html(" 比 赛 结 束 ");
				 		$(".reward1").css("display","block");
				 	}
			 	}
				var signEndTime = new Date(data.sign_end_time).format("MM-dd hh:mm");
				var startTime = new Date(data.start_time).format("MM-dd hh:mm");
				$("#signEndTime").html("报名截止："+signEndTime)
				$("#startTime").html("开赛时间："+startTime)
				 $(".rank").html(data.gameName+" | "+data.region+" | 段位："+data.lowest_rank+"-"+data.highest_rank);
				 $("#matchName").html(data.match_name);
				 $("#ssms").html("赛事描述：段位："+data.lowest_rank+"-"+data.highest_rank);
				 $(".matchPic").html("<img class='matchDetailPic' src='"+data.match_detail_pic+"'/>");
			 
		 },
		 error:function(){
			 alert("加载失败");
		 }
	 })
 })
 
 
  $(function(){
	 $.ajax({
		 url:'../userController/getUserRole',
		 data:{
			 "id":id
		 },
		 success:function(data){
			 console.log(data)
			 if(data.lv!=null && data.lv!="" &&data.lv!="undefined"){
				 $("#rank").html("角色段位："+data.lv);
			 }else{
				 $("#rank").html("角色段位：无");
			 }
			 if(data.wxName!=null && data.wxName!="" &&data.wxName!="undefined"){
				 $("#wxName").html("用户昵称："+data.wxName);
			 }else{
				 $("#wxName").html("用户昵称：无");
			 }
			 if(data.nickname!=null && data.nickname!="" &&data.nickname!="undefined"){
				 $("#nickname").html("角色昵称："+data.nickname);
				 roleNick=data.nickname
			 }else{
				 $("#nickname").html("角色昵称：无");
			 }
			 
		 },
		 error:function(){
			 alert("加载失败");
		 }
	 })
 })
 
 var roleNick='';
 $(".signUP").click(function(){
	 if(piao=="0"){
		 alert("暂无门票")
	 }else{
		 $.ajax({
			 url:'../signUpController/signUp',
			 data:{
				 "id":id,
				 "roleNick":roleNick
			 },
			 success:function(data){
				 alert(data);
				 if(data=="报名成功"){
					 $(".signUP").unbind("click");
			 		 $(".signUP").css("background-color","#cdcdcd");
			 		 $(".signUP").html(" 已 报 名 ");
				 }
				 window.location.href="../html/matchList.html?name_id="+nameId
			 }
		 })
	 }
 })
 
 function updateStart(){
	 $.ajax({
		 url:'../signUpController/updateStart',
		 data:{
			 "id":id
		 },
		 success:function(data){
			 console.log("修改成功");
		 }
	 })
 }
 
 
 
 var appId, timeStamp, nonceStr, packages, signType, paySign;
 function pay(id){
			var url = "../NotifyReqController/NotifyReq?id="+id
			$.get(url,function(result){
				appId = result.appId;
				timeStamp = result.timeStamp;
				nonceStr = result.nonceStr;
				packages = result.package;
				signType = result.signType;
				paySign = result.paySign;
				/* alert(packages);
				alert(signType); */
				if (typeof WeixinJSBridge == "undefined"){
		            if( document.addEventListener ){
		                document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
		            }else if (document.attachEvent){
		                document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
		                document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
		            }
		        }else{
		            onBridgeReady();
		        }
			}) 

}

function onBridgeReady() {
	WeixinJSBridge.invoke('getBrandWCPayRequest', {
		"appId" : appId,
		//公众号名称,由商户传入               
		"timeStamp" : timeStamp,
		//时间戳,自1970年以来的秒数               
		"nonceStr" : nonceStr,
		//随机串              
		"package" : packages,
		"signType" : signType,
		//微信签名方式：               
		"paySign" : paySign
	//微信签名          
	}, function(res) {
		if (res.err_msg == "get_brand_wcpay_request:ok") {
			console.log('支付成功');
			//支付成功后跳转的页面
			window.location.href = "#";
		} else if (res.err_msg == "get_brand_wcpay_request:cancel"){
			//console.log('支付取消');
			window.location.href = "";
		} else if (res.err_msg == "get_brand_wcpay_request:fail") {
			//console.log('支付失败');
			WeixinJSBridge.call('closeWindow');
			window.location.href = "";
		}
		//使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
	});
}
